package com.KKB.dao.impl;

import com.KKB.bean.Student;
import com.KKB.dao.StudentDao;
import com.KKB.util.DBUtils;
import com.KKB.util.StudentEnum;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class StudentDaoImpl extends DBUtils implements StudentDao {
    @Override
    public List<Student> getStudents(String name,String stuno,int sex,int pageIndex,int pageSize) {
        List list = new ArrayList<>();
        List params=new ArrayList();
        try {
            StringBuffer sqlbuff=new StringBuffer("select * from student where 1=1 and state!=4");
            if (name!=null&&name.length()>0){
                sqlbuff.append(" and stuname like ?");
                params.add("%"+name+"%");
            }
            if (stuno!=null&&stuno.length()>0){
                sqlbuff.append(" and stuno= ?");
                params.add(stuno);
            }
            if (sex!=-1){
                sqlbuff.append(" and sex= ?");
                params.add(sex);
            }
            sqlbuff.append(" limit ?,? " );
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);

            ResultSet query = query(sqlbuff.toString(), params);
            while (resultSet.next()){
                Student student=new Student();
                student.setStuId(resultSet.getInt("stuid"));
                student.setStuNo(resultSet.getString("stuno"));
                student.setStuName(resultSet.getString("stuname"));
                student.setSex(resultSet.getInt("sex"));
                student.setPhone(resultSet.getString("phone"));
                student.setProfession(resultSet.getString("profession"));
                student.setRegDate(resultSet.getDate("regdate"));
                list.add(student);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return list;
    }

    @Override
    public int total(String name, String stuno, int sex) {
            int total=0;
        try {
            List params=new ArrayList();
            StringBuffer sqlbuff=new StringBuffer("select count(*) from student where 1=1 and state!=4");
            if (name!=null&&name.length()>0){
                sqlbuff.append(" and stuname like ?");
                params.add("%"+name+"%");
            }
            if (stuno!=null&&stuno.length()>0){
                sqlbuff.append(" and stuno= ?");
                params.add(stuno);
            }
            if (sex!=-1){
                sqlbuff.append(" and sex= ?");
                params.add(sex);
            }
            resultSet= query(sqlbuff.toString(),params);
            while (resultSet.next()){
                total=resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return total;
    }

    @Override
    public int insertStu(Student student) {
        int i=0;
        try {
            String sql="insert into student values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            List param=new ArrayList();
            param.add(student.getStuName());
            param.add(student.getStuNo());
            param.add(student.getSex());
            param.add(student.getPhone());
            param.add(student.getEmail());
            param.add(student.getRegistered());
            param.add(student.getAddress());
            param.add(student.getProfession());
            param.add(student.getIdNumber());
            param.add(student.getPolitics());
            param.add(new Date());
            param.add(StudentEnum.READING.type);
            param.add(student.getIntroduction());
            param.add(student.getGid());
            i = update(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return i;
    }

    @Override
    public Student finById(int sid) {
        Student student = new Student();
        try {
            String sql="select * from student where stuid=?";
            List params=new ArrayList();
            params.add(sid);

            resultSet=query(sql,params);
            while (resultSet.next()){
                student.setStuId(resultSet.getInt("stuid"));
                student.setStuNo(resultSet.getString("stuno"));
                student.setStuName(resultSet.getString("stuname"));
                student.setSex(resultSet.getInt("sex"));
                student.setPhone(resultSet.getString("phone"));
                student.setProfession(resultSet.getString("profession"));
                student.setAddress(resultSet.getString("address"));
                student.setRegDate(resultSet.getDate("regdate"));
                student.setEmail(resultSet.getString("email"));
                student.setIntroduction(resultSet.getString("introduction"));
                student.setGid(resultSet.getInt("gid"));
                student.setRegistered(resultSet.getString("registered"));
                student.setIdNumber(resultSet.getString("idnumber"));
                student.setPolitics(resultSet.getString("politics"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return student;
    }

    @Override
    public int updateStu(Student student) {
        int update = 0;
        try {
            String sql="update student set stuno=?,stuname=?,gid=?,sex=?,email=?,phone=?,profession=?,address=?,politics=?,idnumber=?,profession=?,introduction=? where stuid=?";
            List params=new ArrayList();
            params.add(student.getStuNo());
            params.add(student.getStuName());
            params.add(student.getGid());
            params.add(student.getSex());
            params.add(student.getEmail());
            params.add(student.getPhone());
            params.add(student.getProfession());
            params.add(student.getAddress());
            params.add(student.getPolitics());
            params.add(student.getIdNumber());
            params.add(student.getProfession());
            params.add(student.getIntroduction());
            params.add(student.getStuId());
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public int delStu(String sid) {
        int update = 0;
        try {
            String sql="update student  set state=? where stuid=?";
            List params=new ArrayList();
            params.add(StudentEnum.DELETE.type);
            params.add(sid);
            update = update(sql, params);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return update;
    }

    @Override
    public Student findByStuNo(String stuNo) {
        List params=new ArrayList();
        Student student=new Student();
        try {
            String sql="select * from student where stuno=?";
            params.add(stuNo);
            resultSet = query(sql, params);
            
            if(resultSet.next()){
                student.setStuId(resultSet.getInt("stuid"));
                student.setStuName(resultSet.getString("stuname"));
                student.setStuNo(resultSet.getString("stuno"));
                student.setSex(resultSet.getInt("sex"));
                student.setPhone(resultSet.getString("phone"));
                student.setEmail(resultSet.getString("email"));
                student.setRegistered(resultSet.getString("registered"));
                student.setAddress(resultSet.getString("address"));
                student.setProfession(resultSet.getString("profession"));
                student.setIdNumber(resultSet.getString("idnumber"));
                student.setPolitics(resultSet.getString("politics"));
                student.setRegDate(resultSet.getDate("regdate"));
                student.setState(resultSet.getInt("state"));
                student.setIntroduction(resultSet.getString("introduction"));
                student.setGid(resultSet.getInt("gid"));
                return student;
            }else {
                return null;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return null;
    }
}
